/**
 * 
 */
package com.gopay.bis.manager.bank.ebank;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2bMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcQueryMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * @PsbcB2bManager.java
 * @author Ryan
 * @2013-12-2 上午10:55:59  www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2bManager implements BankManagerRemoteService {
	
    @Resource(name = "psbcB2bBankServiceClientHna")
    private BankService psbcB2bBankServiceClientHna;
	
    @Resource(name = "psbcB2bBankServiceClient")
    private BankService psbcB2bBankServiceClient;
    
    @Resource(name = "psbcConfig")
    private Map<String, String> psbcConfig;
    
    @Override
    public Object sign(BaseMessage message) throws GopayException {
        PsbcB2bMessage psbcB2bMessage = (PsbcB2bMessage) message;
        psbcB2bMessage.setBankUrl(psbcB2bMessage.getPayUrl()); // 请求银行URL
        psbcB2bMessage.setTranAbbr(psbcConfig.get("PSBC.B2B.TRANABBR"));
        psbcB2bMessage.setMasterID(psbcB2bMessage.getCustomerID()); // 企业客户号
        psbcB2bMessage.setAcctNo(message.getCustomerID()); // 企业账号
        psbcB2bMessage.setMercCode(psbcB2bMessage.getMerID());  
        psbcB2bMessage.setMercUrl(psbcB2bMessage.getPayRuturnUrl());
        psbcB2bMessage.setRemark1(psbcConfig.get("PSBC.B2B.REMARK1"));
        psbcB2bMessage.setPlainMessage(buildPsbcPlain(psbcB2bMessage));
        BankLogUtils.printB2BManagerSignSendLog(psbcB2bMessage);
        String signStr;
        if(psbcB2bMessage.isHna()){
        	signStr = (String) psbcB2bBankServiceClientHna.sign(psbcB2bMessage);
        }else{
        	signStr = (String) psbcB2bBankServiceClient.sign(psbcB2bMessage);
        }
        psbcB2bMessage.setSignMessage(signStr);
        BankLogUtils.printB2BManagerSignReturnLog(message);
        return psbcB2bMessage;
    }

    @Override
    public Object verify(BaseMessage message) throws GopayException {
        PsbcB2bMessage psbcB2bMessage = (PsbcB2bMessage) message;
        BankLogUtils.printB2BManagerVerifySendLog(psbcB2bMessage);
        boolean isVerify;
        if(psbcB2bMessage.isHna()){
        	isVerify = (Boolean) psbcB2bBankServiceClientHna.verify(psbcB2bMessage);
        }else{
        	isVerify = (Boolean) psbcB2bBankServiceClient.verify(psbcB2bMessage);
        }
        BankLogUtils.printB2BManagerVerifyReturnLog(psbcB2bMessage, isVerify);
        return isVerify;
    }

    @Override
    public Object query(Object obj) throws GopayException {
        PsbcQueryMessage psbcQueryMessage = (PsbcQueryMessage) obj;
        psbcQueryMessage.setTranAbbr(psbcConfig.get("PSBC.B2B.QUERY.TRANSNAME"));
        psbcQueryMessage.setoTranAbbr(psbcConfig.get("PSBC.B2B.TRANABBR"));
        psbcQueryMessage.setMercCode(psbcConfig.get("PSBC.B2B.MERCCODE"));
        psbcQueryMessage.setPlainMessage(buildQueryPlain(psbcQueryMessage));
        BankLogUtils.printB2BManagerQuerySendLog(psbcQueryMessage, psbcQueryMessage);
        PsbcQueryMessage result;
        if(psbcQueryMessage.isHna()){
        	result = (PsbcQueryMessage) psbcB2bBankServiceClientHna.query(psbcQueryMessage);
        }else{
        	result = (PsbcQueryMessage) psbcB2bBankServiceClient.query(psbcQueryMessage);
        }
        result.setOutStlmCd(BankConstants.BANK_LOGO_PSBC);
        BankLogUtils.printB2BManagerQueryReturnLog(result);
        return result;
    }

    /**
     * 构建B2B签名明文
     * 
     * @param message
     * @return
     */
    private String buildPsbcPlain(PsbcB2bMessage message) {
        // 设置B2B订单有效日期为一周
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(message.getOrderDate()));
        } catch (ParseException e) {
        }
        calendar.add(Calendar.DATE, +7);
        String limitTime = format.format(calendar.getTime());
        StringBuilder sb = new StringBuilder("");
        sb.append("TranAbbr=").append(message.getTranAbbr());
        sb.append("|MasterID=").append(message.getMasterID());
        sb.append("|AcctNo=").append(message.getAcctNo());
        sb.append("|MercDtTm=").append(message.getOrderDate());
        sb.append("|TermSsn=").append(message.getOrderId());
        sb.append("|OSttDate=").append(message.getoSttDate());
        sb.append("|OAcqSsn=").append(message.getoAcqSsn());
        sb.append("|MercCode=").append(message.getMercCode());
        sb.append("|TermCode=").append(message.getTermCode());
        sb.append("|TranAmt=").append(message.getAmount().toString());
        sb.append("|MercUrl=").append(message.getMercUrl());
        sb.append("|Remark1=").append(message.getRemark1());
        sb.append("|Remark2=").append(message.getRemark2());
        sb.append("|LimitTime=").append(limitTime);
        return sb.toString();
    }
    /**
     * 构建订单查询明文
     * 
     * @param message
     * @return
     */
    private String buildQueryPlain(PsbcQueryMessage psbcQueryMessage) {
        StringBuffer sf = new StringBuffer("");
        sf.append("OTranAbbr=").append(psbcQueryMessage.getoTranAbbr());
        sf.append("|MercCode=").append(psbcQueryMessage.getMercCode());
        sf.append("|TermSsn=").append(psbcQueryMessage.getTermSsn());
        return sf.toString();
    }

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}
}
